package com.supeng.PMdepot.activity;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.supeng.PMdepot.R;
import com.supeng.PMdepot.base.BaseActivity;
import java.util.Set;

import zpSDK.zpSDK.zpBluetoothPrinter;

/**
 * Created by sp on 2018/12/14.
 * 蓝牙打印机
 */

public class PrinterActivity extends BaseActivity{
    public static BluetoothAdapter myBluetoothAdapter;
    public String SelectedBDAddress;
    private TextView tv_connectBlooth;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_printer);
        tv_connectBlooth=findViewById(R.id.tv_connectBlooth);
        SelectedBDAddress=ListBluetoothDevice();
        if(TextUtils.isEmpty(SelectedBDAddress)){
            finish();
        }
        Button Button1 = (Button) findViewById(R.id.button1);
        Button1.setOnClickListener(new Button.OnClickListener()
        {
            public void onClick(View arg0) {
                Print1(SelectedBDAddress);
            }
        });
    }

    private void Print1(String BDAddress) {
        zpBluetoothPrinter zpSDK=new zpBluetoothPrinter(this);
        if(!zpSDK.connect(BDAddress))
        {
            Toast.makeText(this,"连接失败------", Toast.LENGTH_LONG).show();
            return;
        }
        zpSDK.pageSetup(560, 350);
        /*zpSDK.drawText(32, 5, "流程单号： L1811001", 3, 0, 0, false, false);
        zpSDK.drawText(456, 9, "合格", 3, 0, 0, false, false);
        zpSDK.drawText(32, 57, "产品名称：test", 3, 0, 0, false, false);
        zpSDK.drawText(32, 109, "产品规格:1/2,Hex10,L20", 3, 0, 0, false, false);
        zpSDK.drawText(32, 168, "工序名称 冷镦", 3, 0, 0, false, false);
        zpSDK.drawText(32, 227, "2019/11/29 20:08:54", 2, 0, 0, false, false);
        zpSDK.drawQrCode(398, 113, "111111111", 0, 3, 0);*/
        zpSDK.drawText(32, 5, "提需求请付款", 4, 0, 0, false, false);
        zpSDK.drawText(27, 92, "微信付款" , 3, 0, 0, false, false);
        zpSDK.drawText(48, 135, "谢谢！" , 3, 0, 0, false, false);
        zpSDK.drawQrCode(322, 37, "wxp://f2f0yGCt10Qz36K3shSRXCCi9HORxaRGHX09", 0, 3, 0);

       // zpSDK.drawBarCode(124,48+100+56+56+80+80+80 , "12345678901234567", 128, false, 3, 60);
        zpSDK.print(0, 0);

        zpSDK.printerStatus();
        int a=zpSDK.GetStatus();

        if(a==-1)
        { //"获取状态异常------";
            Toast.makeText(this,"获取状态异常-----", Toast.LENGTH_LONG).show();


        }
        if(a==1)
        {//"缺纸----------";
            Toast.makeText(this,"缺纸----------", Toast.LENGTH_LONG).show();
        }
        if(a==2)
        {
            //"开盖----------";
            Toast.makeText(this,"开盖----------", Toast.LENGTH_LONG).show();
        }
        if(a==0)
        {

            //"打印机正常-------";
            Toast.makeText(this,"打印机正常-------", Toast.LENGTH_LONG).show();
        }


        zpSDK.disconnect();
    }

    public String ListBluetoothDevice()
    {


        if((myBluetoothAdapter = BluetoothAdapter.getDefaultAdapter())==null)
        {
            Toast.makeText(this,"没有找到蓝牙适配器", Toast.LENGTH_LONG).show();
            return "";
        }

        if(!myBluetoothAdapter.isEnabled())
        {
            Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBtIntent, 2);
        }

        Set<BluetoothDevice> pairedDevices = myBluetoothAdapter.getBondedDevices();
        if (pairedDevices.size() <= 0)return "";
        for (BluetoothDevice device : pairedDevices)
        {
            if(device.getName().indexOf("XT")!=-1){
                tv_connectBlooth.setText(device.getName());
                return device.getAddress();

            }

        }
        return "";
    }
}
